package http

import (
	"fmt"
	"log-record/utils"
	"net/http"
)

func init() {
	http.Handle("/", http.FileServer(http.Dir("./public")))
	listenPort := utils.EnvGetOrDefaultStringValue("HTTP_LISTEN_PORT", "8080")
	http.HandleFunc("/remoteLogs", queryRemoteLogs)
	http.HandleFunc("/remoteLogApplications", queryRemoteAllApp)
	http.HandleFunc("/remoteLogAllIp", queryRemoteAllIp)
	http.HandleFunc("/remoteLogAllLevel", queryRemoteAllLevel)
	go func() {
		err := http.ListenAndServe(fmt.Sprintf(":%v", listenPort), nil)
		if err != nil {
			panic(err)
		}
	}()
	fmt.Println(fmt.Sprintf("init http server,HTTP_LISTEN_PORT %v \r\n", listenPort))
}

func writeJson(result *Result, res *http.ResponseWriter) {
	response := *res
	header := response.Header()
	header.Set("content-type", "application/json;charset=utf-8")
	response.WriteHeader(200)
	_, _ = response.Write(result.ToJsonBytes())
}
